import time
import json
from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
from core import settings
from lib import conf_common

"""获取cookie"""


class Testcookie:
    """获取配置文件"""
    base = conf_common.UseCofg(settings.Cfg_path)

    def setup(self):

        cookie_url = self.base.use_cofg("base_address", "base_address_url") + "/wchl-web/html/login"
        self.driver = webdriver.Firefox()
        self.driver.get(cookie_url)
        self.driver.maximize_window()
        self.driver.implicitly_wait(6)
        sleep(3)

    def teardown(self):
        self.driver.quit()

    def get_cookies(self):
        """输入用户名"""
        self.driver.find_element(By.XPATH,
                                 '//*[@id="app"]/div/div[2]/div/div[2]/form/div[1]/div/div[1]/input').send_keys(
            self.base.use_cofg("user", "shopping_phone"))
        """输入密码"""
        self.driver.find_element(By.XPATH,
                                 '/html/body/div/div/div[2]/div/div[2]/form/div[2]/div[1]/div/div[1]/input').send_keys(
            self.base.use_cofg("user", "identifying_code"))
        time.sleep(2)
        """点击"""
        self.driver.find_element(By.XPATH, '//*[@id="app"]/div/div[2]/div/div[3]/button').click()
        time.sleep(2)
        """点击选择公司"""
        self.driver.find_element(By.XPATH,'//*[@id="app"]/div/div[5]/div/div[2]/form/div/div/div/div/input').click()
        time.sleep(2)
        """选择肇庆"""
        self.driver.find_element(By.XPATH,"/html/body/div[3]/div[1]/div[1]/ul/li[7]/span").click()
        time.sleep(2)
        """点击确定"""
        self.driver.find_element(By.XPATH,'//*[@id="app"]/div/div[5]/div/div[3]/span/button[2]/span').click()
        time.sleep(2)
        sleep(7)
        """获取cookie"""
        # self.driver.get('http://mall.wancaihulian-dev.com/wchl-web/html/home')
        cookies = self.driver.get_cookies()
        print(cookies)
        json_cookies = json.dumps(cookies)
        print(type(json_cookies))
        with open(r"C:\Users\13399\IdeaProjects\wancaihulian11\cookie\cookie.json","w") as f:
            f.write(json_cookies)




# a = Testcookie()
# a.setup()
#
# print(a.get_cookies())
# a.teardown()

